package cz.najman.emdroid.helper;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import cz.najman.emdroid.connection.ConnectionMethod;
import cz.najman.emdroid.mappings.Event;
import cz.najman.emdroid.mappings.EventInstitutionManager;
import cz.najman.emdroid.mappings.User;
import cz.najman.emdroid.service.ForwardingResultReceiver;
import cz.najman.emdroid.service.UpdaterService;
import cz.najman.emdroid.utils.WhereClauseBuilder;

public class EventHelper extends AbstractHelper {

	
	public EventHelper(Context context, ForwardingResultReceiver receiver) {
		super(context, receiver);		
	}

	private static final String TAG = "EventHelper";	
	
	/* ***************************************************************
	 * 					REMOTE HELPER METHODS 						 *
	 * ***************************************************************/
	
	public void retrieveUsersRegisteredEvents(Integer userId){
		Log.d(TAG, "> retrieveUsersRegisteredEvents");		
		Intent intent = getIntentWithExtra(
				Event.CONTENT_URI_EVENTS_REGISTERED,
				ConnectionMethod.METHOD_GET,
				new String[] {User.URI_TEMPLATE_STRING},
				new String[] {userId.toString()},
				null
			);        
        mContext.startService(intent);
	}
	
	public void retrieveUsersAvailableEvents(Integer institutionId){
		Log.d(TAG, "> retrieveUsersAvailableEvents");
		Intent intent = getIntentWithExtra(
				Event.CONTENT_URI_EVENTS_AVAILABLE,
				ConnectionMethod.METHOD_GET,
				new String[] {User.URI_TEMPLATE_STRING},
				new String[] {institutionId.toString()},
				null
			);
        mContext.startService(intent);
	}
	
	public void retrieveUsersManageableEvents(Integer userId){
		Log.d(TAG, "> retrieveUsersManageableEvents");
		Intent intent = getIntentWithExtra(
				Event.CONTENT_URI_EVENTS_MANAGEABLE,
				ConnectionMethod.METHOD_GET,
				new String[] {User.URI_TEMPLATE_STRING},
				new String[] {userId.toString()},
				null
			);
        mContext.startService(intent);
	}
	
	public void retrieveEvent(Integer eventId){
		Log.d(TAG, "> retrieveEvent");
		Intent intent = getIntentWithExtra(
				Uri.withAppendedPath(Event.CONTENT_URI_EVENTS, eventId.toString()),
				ConnectionMethod.METHOD_GET,
				new String[] {Event.URI_TEMPLATE_STRING},
				new String[] {eventId.toString()},
				null
			);
        mContext.startService(intent);
	}
	
	@Override
	protected Intent getIntentWithExtra(Uri uri, int method, String[] parameterTypes, String[] parameterValues, ContentValues values) {
		Intent intent = new Intent(mContext, UpdaterService.class);
		intent.putExtra(UpdaterService.RESULT_RECEIVER,	mResultReceiver);
		intent.putExtra(UpdaterService.OBJECT, Event.class);
		intent.putExtra(UpdaterService.URI, uri);
		intent.putExtra(UpdaterService.METHOD, method);
		if (parameterTypes != null) {
			intent.putExtra(UpdaterService.PARAMETER_TYPES, parameterTypes);
			intent.putExtra(UpdaterService.PARAMETER_VALUES, parameterValues);
		}
		if (values != null) {
			intent.putExtra(UpdaterService.CONTENT_VALUES, values);
		}
		return intent;
	}
	
	/* ***************************************************************
	 * 					DATABASE HELPER METHODS 					 *
	 * ***************************************************************/
	
	public Cursor getEventById(Integer eventId, String[] projection){
		return mContentResolver.query(Uri.withAppendedPath(Event.CONTENT_URI_EVENTS, eventId.toString()), projection, null, null, null);
	}
	
	public Cursor getUsersRegisteredEvents(String[] projection, String selection, String sortOrder){
		
		return mContentResolver.query(Event.CONTENT_URI_EVENTS_REGISTERED, projection, selection, null, sortOrder);
	}
	
	public Cursor getUsersManageableEvents(String[] projection, String selection, String sortOrder){
		return mContentResolver.query(Event.CONTENT_URI_EVENTS_MANAGEABLE, projection, selection, null, sortOrder);
	}
	
	public Cursor getUsersAvailableEvents(int institutionId, String[] projection, String selection, String sortOrder){	
		selection = mWCB.buildWhereClause(EventInstitutionManager.INSTITUTION_ID, institutionId, WhereClauseBuilder.EQUAL);
		
		return mContentResolver.query(Event.CONTENT_URI_EVENTS_AVAILABLE, projection, selection, null, sortOrder);
	}
	
}
